//
//  BKMyOrderLogisticsView.m
//  bestkeep
//
//  Created by mac on 16/12/15.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKMyOrderLogisticsView.h"
#import "BK_OD_Tool.h"
#import "BK_OD_Color.h"


@interface BKMyOrderLogisticsView ()

@property (nonatomic, strong) UIButton *titleButton;
@property (nonatomic, strong) UIScrollView *rootScrollView;


@end

@implementation BKMyOrderLogisticsView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _currentIndex = 0;
        _lineEdgeInsets = UIEdgeInsetsMake(0, 3, 2, 3);
        _cursorEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        
        [self initView];
        
    }
    return self;
}

-(UIView*)lineView
{
    if (!_lineView) {
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = [UIColor blackColor];
        [self.titleScrollView addSubview:_lineView];
    }
    return _lineView;
}

-(void)initView{
    
    [self setTranslatesAutoresizingMaskIntoConstraints:YES];
    UIView *view = [UIView new];
    view.backgroundColor = RGB(242, 242, 242);
    [self addSubview:view];
    /*
     * 第一个灰色框
     */
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top);
        make.left.mas_equalTo(self.mas_left);
        make.right.mas_equalTo(self.mas_right);
        make.bottom.mas_equalTo(self.mas_top).offset(10);
    }];
    
    /*
     * 订单号
     */
    UILabel *orderNumber = [UILabel new];
    orderNumber.text = @"订单号:";
    orderNumber.font = [UIFont systemFontOfSize:15.0];
    orderNumber.textColor = kOrderBlack1;
    [self addSubview:orderNumber];
    [orderNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(view.mas_bottom);
        make.left.mas_equalTo(self.mas_left).offset(ApplicationBackGauge);
        make.bottom.mas_equalTo(view.mas_bottom).offset(45);
        make.width.mas_equalTo(@60);
    }];
    
    _orderLabel = [[UILabel alloc] init];
//    _orderLabel.backgroundColor=[UIColor   yellowColor];
    _orderLabel.font = [UIFont systemFontOfSize:16.0];
    _orderLabel.textColor = kOrderBlack1;
    [self addSubview:_orderLabel];
    [_orderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(view.mas_bottom);
        make.left.mas_equalTo(orderNumber.mas_right);
        make.right.mas_equalTo(self.mas_right);
        make.height.mas_equalTo(@45);
    }];
    
    UIView *view1 = [UIView new];
    view1.backgroundColor = RGB(242, 242, 242);
    [self addSubview:view1];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.mas_top).offset(50);
        make.top.mas_equalTo(self.mas_top).offset(55);
        make.left.mas_equalTo(self.mas_left);
        make.right.mas_equalTo(self.mas_right);
        make.height.mas_equalTo(@10);
    }];
    
    _titleScrollView = [UIScrollView new];
    _titleScrollView.backgroundColor = [UIColor whiteColor];
    _titleScrollView.contentOffset = CGPointMake(0, ScreenWidth);
    [self addSubview:_titleScrollView];
    [_titleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left);
        make.right.mas_equalTo(self.mas_right);
        make.top.mas_equalTo(view1.mas_bottom);
        make.height.mas_equalTo(@36);
    }];
    
    UIView *line1 = [UIView new];
    line1.backgroundColor = RGB(242, 242, 242);
    [_titleScrollView addSubview:line1];
    
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleScrollView.mas_bottom).offset(-1);
        make.left.mas_equalTo(self.mas_left);
        make.right.mas_equalTo(self.mas_right);
        make.height.mas_equalTo(@(1));
    }];
    
    UILabel *company = [UILabel new];
    company.textColor = kOrderBlack1;
    company.text = @"快递公司:";
    company.font = [UIFont systemFontOfSize:14.0];
    [self addSubview:company];
    
    [company mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleScrollView.mas_bottom).offset(15);
        make.left.mas_equalTo(self.mas_left).offset(ApplicationBackGauge);
        make.width.mas_equalTo(@70);
    }];
    
    _companyLabel = [UILabel new];
    _companyLabel.textColor = kOrderBlack1;
    _companyLabel.font = [UIFont systemFontOfSize:14.0];
    [self addSubview:_companyLabel];
    
    [_companyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleScrollView.mas_bottom).offset(15);
        make.left.mas_equalTo(company.mas_right);
    }];
    
    UILabel *detailLabel = [UILabel new];
    detailLabel.textColor = kOrderBlack1;
    detailLabel.text = @"快递单号:";
    detailLabel.font = [UIFont systemFontOfSize:14.0];
    [self addSubview:detailLabel];
    
    [detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_companyLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(self.mas_left).offset(ApplicationBackGauge);
        make.width.mas_equalTo(@70);
    }];
    
    
    _logisticsOrderNO = [BKOrderLabel new];
    _logisticsOrderNO.textColor = kOrderBlack1;
    _logisticsOrderNO.font = [UIFont systemFontOfSize:15.0];
    [self addSubview:_logisticsOrderNO];
    
    [_logisticsOrderNO mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_companyLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(detailLabel.mas_right);
    }];
    
    _line = [UILabel new];
    _line.backgroundColor = RGB(242, 242, 242);
    [self addSubview:_line];
    
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(detailLabel.mas_bottom).offset(15);
        make.left.mas_equalTo(self.mas_left).offset(ApplicationBackGauge);
        make.right.mas_equalTo(self.mas_right).offset(-ApplicationBackGauge);
        make.height.mas_equalTo(@(1));
    }];
    
    UIView *noticeView = [UIView new];
    noticeView.backgroundColor = RGB(242, 242, 242);
    [self addSubview:noticeView];
    [noticeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_line.mas_bottom).offset(15);
        make.left.mas_equalTo(self.mas_left).offset(ApplicationBackGauge);
        make.right.mas_equalTo(self.mas_right).offset(-ApplicationBackGauge);
        make.height.mas_equalTo(@52);
    }];
    
    _noticLabel = [UILabel new];
    _noticLabel.backgroundColor = RGB(242, 242, 242);
    _noticLabel.textColor = kOrderBlack1;
    _noticLabel.font = [UIFont systemFontOfSize:12.0];
    _noticLabel.numberOfLines = 0;
    [self addSubview:_noticLabel];
    
    [_noticLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_line.mas_bottom).offset(15);
        make.left.mas_equalTo(self.mas_left).offset(20);
        make.right.mas_equalTo(self.mas_right).offset(-20);
        make.height.mas_equalTo(@52);
    }];
    
    UILabel *followLogistics = [UILabel new];
    followLogistics.text = @"物流跟踪";
    followLogistics.font = [UIFont systemFontOfSize:15.0];
    [self addSubview:followLogistics];
    [followLogistics mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_noticLabel.mas_bottom).offset(12);
        make.left.mas_equalTo(self.mas_left).offset(ApplicationBackGauge);
        make.height.mas_equalTo(@30);
    }];
    
}

-(void)setTitles:(NSArray *)titles
{
    _titles = titles;
    for (NSInteger i = 0; i < self.titles.count; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10 + 50 * i, 0, 50, 30)];
        btn.tag = i+99;
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn setTitle:self.titles[i] forState:UIControlStateNormal];
        [btn setTitleColor:kOrderLightGrey1 forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnTouch:) forControlEvents:UIControlEventTouchUpInside];
        [_titleScrollView addSubview:btn];
        if (i == 0) {
            self.lineView.frame = CGRectMake(10, 31, 50, 3);
            [btn setTitleColor:kOrderBlack1 forState:UIControlStateNormal];
        }
    }
}


-(void)btnTouch:(UIButton*)sender{
    
    _currentIndex = sender.tag;
    [self resizeLineViewWihtCellFrame:sender.frame animated:YES];
    _companyLabel.text = [NSString stringWithFormat:@"%@", _logisticsArray[sender.tag-99]];
    _logisticsOrderNO.text = [NSString stringWithFormat:@"%@", _logisticsNumberArray[sender.tag-99]];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updageLogicMsg" object:nil];
    NSMutableArray *array = [NSMutableArray array];
    for (NSInteger i = 0; i < self.titles.count; i++) {
        
        UIButton *btn = (UIButton*)[self viewWithTag:i+99];
        
        [array addObject:btn];
        if (i == sender.tag - 99) {
            [array[i] setTitleColor:kOrderBlack1 forState:UIControlStateNormal];
        }else{
            [array[i] setTitleColor:kOrderLightGrey1 forState:UIControlStateNormal];
        }
    }
}

-(void)resizeLineViewWihtCellFrame:(CGRect)frame animated:(BOOL)animated
{
    CGFloat height = 3.0f;
    CGRect rect = CGRectMake(CGRectGetMinX(frame),
                             CGRectGetHeight(self.titleScrollView.frame)-height-_lineEdgeInsets.bottom,
                             CGRectGetWidth(frame), height-_lineEdgeInsets.top);
    if (animated) {
        [UIView animateWithDuration:0.3f animations:^{
            self.lineView.frame = rect;
        }];
    }else{
        self.lineView.frame = rect;
    }
}

@end

